package com.imooc.framework.basic.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ReflectSample {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, InvocationTargetException, NoSuchMethodException, NoSuchFieldException {
        Class rc = Class.forName("com.imooc.framework.basic.reflect.Robot");
        com.imooc.framework.basic.reflect.Robot r = (com.imooc.framework.basic.reflect.Robot) rc.newInstance();
        System.out.println("Class name is " + rc.getName());
        Method getHello = rc.getDeclaredMethod("throwHello", String.class);
        getHello.setAccessible(true);
        Object str = getHello.invoke(r, "Bob");
        System.out.println("getHello result is " + str);
        Method sayHi = rc.getMethod("sayHi", String.class);
        sayHi.invoke(r, "Welcome");
        Field name = rc.getDeclaredField("name");
        name.setAccessible(true);
        name.set(r, "Alice");
        sayHi.invoke(r, "Welcome");
        System.out.println(System.getProperty("java.ext.dirs"));
        System.out.println(System.getProperty("java.class.path"));

    }

//    public static void main(String[] args) throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
//        Class rc = Robot.class;
//        Robot r = (Robot) rc.newInstance();
//
//        r.sayHi("Bob");
//        Method getHello = rc.getDeclaredMethod("throwHello",String.class);
//        getHello.setAccessible(true);
//        Object str = getHello.invoke(r,"Bob");
//        System.out.println(str);
//
//        Method sayHi = rc.getMethod("sayHi",String.class);
//        sayHi.invoke(r,"welcome");
//
//        Field name = rc.getDeclaredField("name");
//        name.setAccessible(true);
//        name.set(r,"Alice");
//        sayHi.invoke(r,"welcome");
//    }
}
